﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeArt.SharePoint.Configuration
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System.Configuration;

    public sealed class ViewFieldElement :  NameValueElement
    {
        [ConfigurationProperty("fieldType", IsRequired = false, DefaultValue = "System.String")]
        public string FieldType
        {
            get { return (string)this["fieldType"]; }
        }   

        public string FieldPrev
        {
            get
            {
                string fn = this.Name;
                int i = fn.IndexOf(',');
                if (i > 0)
                {
                    return fn.Substring(0, i+1);
                }
                return string.Empty;
            }
        }

        public string FieldName
        { 
            get { string fn = this.Name ;
                  int i =fn.IndexOf(',');
                  if(i>0)
                  {
                    return fn.Substring(i+1,fn.Length);
                  }
                  return fn;
                }
        }

        [ConfigurationProperty("columnWidth", IsRequired = false, DefaultValue = "")]
        public string ColumnWidth
        {
            get { return (string)this["columnWidth"]; }
        }

        public ViewFieldElement() { }
    }


    public class ViewFieldsElement : ConfigurationElement
    {

        /// <summary>
        /// ViewFields
        /// </summary>
        [ConfigurationProperty("fields", IsDefaultCollection = true, IsRequired = true)]
        public ViewFieldCollection Fields
        {
            get
            {
                return (ViewFieldCollection)base["fields"];
            }
        }

        /// <summary>
        /// lookupIDField
        /// </summary>
        [ConfigurationProperty("lookupIDField", IsRequired = true)]
        public string LookupIDField
        {
            get { return (string)base["lookupIDField"]; }
        }

        /// <summary>
        /// lookupNameField
        /// </summary>
        [ConfigurationProperty("lookupNameField", IsRequired = true)]
        public string LookupNameField
        {
            get { return (string)base["lookupNameField"]; }
        }
    }

    public class ViewFieldCollection : ConfigurationElementCollection
	{
        public  ViewFieldElement this[object key]
        {
            get
            {
                return base.BaseGet(key) as ViewFieldElement;
            }
        }

        public  ViewFieldElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as ViewFieldElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                //
                this.BaseAdd(index, value);
            }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        public ViewFieldCollection() { }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ViewFieldElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ViewFieldElement)element).Name;
        }

        protected override string ElementName
        {
            get
            {
                return "field";
            }
        }
        
	}
}
